package com.wlyuan.open.scanner.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlyuan.open.scanner.dao.entity.OrderContainsDO;
import com.wlyuan.open.scanner.dao.mapper.OrderContainsMapper;
import com.wlyuan.open.scanner.dao.mapper.OrderIndexMapper;
import com.wlyuan.open.scanner.domain.order.OrderContain;
import com.wlyuan.open.scanner.domain.order.OrderContainConvert;
import com.wlyuan.open.scanner.domain.quotation.CombineQuotationSpec;
import com.wlyuan.open.scanner.repository.OrderContainRepository;
import com.wlyuan.open.scanner.utils.BatchSelectUtils;
import com.wlyuan.open.scanner.utils.StreamUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderContainRepositoryImpl implements OrderContainRepository {
    private final OrderContainsMapper orderContainsMapper;
    private final OrderIndexMapper orderIndexMapper;

    @Override
    public Map<Long, List<Long>> getOrderContainByOrderIds(List<Long> orderIds) {
        if (CollectionUtils.isEmpty(orderIds)) {
            return Collections.emptyMap();
        }
        orderIds = orderIds.stream().distinct().collect(Collectors.toList());
        //分批查询
        var list = BatchSelectUtils.batch(orderIds, this::getSubList);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        var result = StreamUtils.convert(list, OrderContainConvert::convert);
        return StreamUtils.groupBy(result, OrderContain::getOwnerOrderId, OrderContain::getOrderId);
    }

    @Override
    public List<Long> getOrderContainByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderContainsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderContainsDO::getContainsOwnerorderid, orderId);
        var list = orderContainsMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return StreamUtils.mapList(list, OrderContainsDO::getContainsOrderid);
    }

    private List<OrderContainsDO> getSubList(List<Long> orderIds) {
        LambdaQueryWrapper<OrderContainsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OrderContainsDO::getContainsOwnerorderid, orderIds);
        return orderContainsMapper.selectList(wrapper);
    }

    @Override
    public List<CombineQuotationSpec> getOrderSpecByOrderId(Long orderId) {
        var ids = getOrderContainByOrderId(orderId);
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return ids.stream().map(id -> {
            var order = orderIndexMapper.getQuotationOrderByOrderId(id);
            return Optional.ofNullable(order).map(o -> OrderContainConvert.convert(o)).orElse(null);
        }).collect(Collectors.toList()).stream().filter(Objects::nonNull).collect(Collectors.toList());
    }
}
